#include "unp.h"

int my_open(char* pathname, int mode)
{
  int fd, sockfd[2], status;
  pid_t childpid;
  char c, argsockfd[10],argmode[10];

  socketpair(AF_LOCAL,SOCK_STREAM,0,sockfd);
  childpid = fork();
  if (childpid == 0)
  {  
    close(sockfd[0]);
    snprintf(argsockfd,sizeof(argsockfd),"%d",sockfd[1]);
    snprintf(argmode,sizeof(argmode),"%d",mode);
    execl("./openfile","openfile",argsockfd,pathname,argmode,(char*)NULL);
    printf("open file error\n");
  }
  
  close(sockfd[1]);
  waitpid(childpid,&status,0);
  if (WIFEXITED(status) == 0)
  {
    printf("child not terminate\n");
    exit(-1);
  }
  if ( (status = WEXITSTATUS(status)) == 0)
  {
    read_fd(sockfd[0],&c,1,&fd);
  }
  else
  {
    errno = status;
    fd = -1;
  }
  
  close(sockfd[0]);

  return fd;
}


